package com.example.javabase.multithread.unreentrantlock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

/**
 * 不可重入锁
 * @author He Changjie on 2021/7/10
 */
public class UnReentrantLock implements Lock {
    private Thread thread;

    @Override
    public void lock() {
        synchronized (this){
            while (thread != null){
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            this.thread = Thread.currentThread();
        }
    }

    @Override
    public void lockInterruptibly() throws InterruptedException {

    }

    @Override
    public boolean tryLock() {
        return false;
    }

    @Override
    public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
        return false;
    }

    @Override
    public void unlock() {
        synchronized (this){
            if (Thread.currentThread() != thread){
               return;
            }
            this.thread = null;
            notifyAll();
        }
    }

    @Override
    public Condition newCondition() {
        return null;
    }
}
